package com.telecom.restaurant.controllor.manage;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.ManageThreadLocal;
import com.telecom.restaurant.dao.CouponCustomerDao;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.model.CouponCustomerPO;
import com.telecom.restaurant.service.CouponService;
import com.telecom.restaurant.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.Date;

import static com.telecom.restaurant.constant.DefaultConstant.TEMP_EXCEL_PATH;

/**
 * Created by qiqi on 2019/12/14.
 */
@Controller
@RequestMapping("/restaurant/coupon")
public class RestaurantCouponController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponCustomerDao couponCustomerDao;


    @RequestMapping("/uploadList")
    @ResponseBody
    public DataResult uploadImg(@RequestParam("file") MultipartFile file) {

        // 随机产生文件名
        String fileName = file.getOriginalFilename();

        try {
            File filePath = new File(TEMP_EXCEL_PATH);
            if (!filePath.exists()) {
                filePath.mkdir();
            }

            String newFilePath = TEMP_EXCEL_PATH + File.separator + System.currentTimeMillis() + "_" + fileName;
            File newFile = new File(newFilePath);
            logger.info("update excel path to {}", newFile.getAbsoluteFile());
            file.transferTo(newFile);

            couponService.dealExcelFile(newFile);

        } catch (Exception e) {
            logger.error("uploadImg error", e);
        }
        DataResult dataResult = DataResultConstant.buildSuccessResult(null);
        return dataResult;
    }


    @RequestMapping("/search")
    @ResponseBody
    public DataResult search(String mobile) {

        if (StringUtils.isBlank(mobile)) {
            return DataResultConstant.WRONG_PARAM;

        }
        CouponCustomerPO couponCustomerPO = couponCustomerDao.searchByMobile(mobile);
        DataResult dataResult = DataResultConstant.buildSuccessResult(couponCustomerPO);
        return dataResult;
    }

    @RequestMapping("/exportList")
    public void exportList(HttpServletResponse response) {

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        try {
            String fileName = "优惠券名单列表";
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
            couponService.exportCouponList(restaurantId, response.getOutputStream());
        } catch (Exception e) {
            logger.error("下载失败", e);
        }
    }

}
